var base64 = require("../weui/example/images/base64");
//获取应用实例
const app = getApp()

Page({
  data: {
    userId: null,
    klArr: [],
    pageNo: 1,
    pageSize: 20,
    display: true,
    knowledgeType: -1,
    knowledgeTit: null,
    lookMore: '数据加载中...',
    wh:0
  },
  todetail: function (e) {
    wx.navigateTo({
      url: '../klDetail/klDetail?id=' + e.currentTarget.dataset.id
    })
  },
  goBack: function(){
    wx.navigateBack({
      delta: 1 //返回的页面数，如果 delta 大于现有页面数，则返回到首页,
    });
  },
  onLoad: function (options) {
    var knowledgeType = options.knowledgeType;
    var knowledgeTit = options.knowTit;
    wx.setNavigationBarTitle({
      title: knowledgeTit
    })
    this.setData({
      knowledgeType: knowledgeType,
      knowledgeTit: knowledgeTit,
      search: this.search.bind(this)
    });
    if (app.globalData.userId) {
      var that = this;
      this.setData({
        userId: app.globalData.userId
      })
      wx.request({
        url: 'https://www.xinhuo.site/api/knowledge/list',
        data: {
          pageNo: this.data.pageNo,
          pageSize: this.data.pageSize,
          knowledgeType: this.data.knowledgeType
        },
        method: 'POST',
        header: {
          'Content-Type': 'application/json'
        },
        success(res) {
          if (res.data.syscode == 0) {
            if (res.data.recordList.length == that.data.pageSize) {
              that.data.lookMore = '-上滑查看更多-'
            }
            else {
              that.data.lookMore = '-没有更多内容-'
            }
            that.data.klArr = that.data.klArr.concat(res.data.recordList)
            that.setData({
              klArr: that.data.klArr,
              lookMore: that.data.lookMore
            })
          }
          else {
            that.setData({
              lookMore: '-暂无知识-'
            })
          }
        }
      })
    }
  },
  onShow: function () {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          "wh": res.windowHeight - 96 -wx.getSystemInfoSync().statusBarHeight
        })
      }
    })
  },
  search: function (value) {
    return new Promise((resolve, reject) => {
      if (this.data.i % 2 === 0) {
        setTimeout(() => {
          resolve([{ text: '搜索结果', value: 1 }, { text: '搜索结果2', value: 2 }])
        }, 200)
      } else {
        setTimeout(() => {
          resolve([])
        }, 200)

      }
      this.setData({
        i: this.data.i + 1
      })
    })
  },
  selectResult: function (e) {
    console.log('select result', e.detail)
  }
})
